package com.zz.qimo;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.zz.qimo.sqlite.DatabaseHelper;

public class ChangePasswordActivity extends AppCompatActivity {
    private EditText oldPasswordEditText, newPasswordEditText, confirmNewPasswordEditText;
    private Button changePasswordButton;

    private DatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_password);

        // 初始化视图和数据库帮助类
        oldPasswordEditText = findViewById(R.id.editTextOldPassword);
        newPasswordEditText = findViewById(R.id.editTextNewPassword);
        confirmNewPasswordEditText = findViewById(R.id.editTextConfirmNewPassword);
        changePasswordButton = findViewById(R.id.buttonChangePassword);

        dbHelper = new DatabaseHelper(this);

        // 修改密码按钮点击事件
        changePasswordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String oldPassword = oldPasswordEditText.getText().toString();
                String newPassword = newPasswordEditText.getText().toString();
                String confirmNewPassword = confirmNewPasswordEditText.getText().toString();

                if (newPassword.equals(confirmNewPassword)) {
                    String username = getUsernameFromSharedPreferences(); // 从SharedPreferences中获取用户名

                    if (username.isEmpty()) {
                        Toast.makeText(ChangePasswordActivity.this, "未找到用户名", Toast.LENGTH_SHORT).show();
                    } else if (oldPassword.isEmpty() || newPassword.isEmpty() || confirmNewPassword.isEmpty()) {
                        Toast.makeText(ChangePasswordActivity.this, "请填写完整信息", Toast.LENGTH_SHORT).show();
                    } else if (!isValidUser(username, oldPassword)) {
                        Toast.makeText(ChangePasswordActivity.this, "旧密码不正确", Toast.LENGTH_SHORT).show();
                    } else {
                        updatePassword(username, newPassword);
                    }
                } else {
                    Toast.makeText(ChangePasswordActivity.this, "新密码不匹配", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private String getUsernameFromSharedPreferences() {
        SharedPreferences preferences = getSharedPreferences("YOUR_PREFS_NAME", MODE_PRIVATE);
        return preferences.getString("username", ""); // 返回用户名或者默认值
    }

    private boolean isValidUser(String username, String password) {
        return dbHelper.isValidUser(username, password); // 调用数据库助手类的验证用户方法
    }

    private void updatePassword(String username, String newPassword) {
        int rowsAffected = dbHelper.updateUserPassword(username, newPassword); // 调用数据库助手类的更新用户密码方法

        if (rowsAffected > 0) {
            Toast.makeText(ChangePasswordActivity.this, "密码修改成功", Toast.LENGTH_SHORT).show();
            navigateToLogin(); // 密码修改成功，跳转到登录页面
        } else {
            Toast.makeText(ChangePasswordActivity.this, "密码修改失败", Toast.LENGTH_SHORT).show();
        }
    }

    private void navigateToLogin() {
        // 返回登录页面
        Intent intent = new Intent(ChangePasswordActivity.this, LoginActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);
        finish();
    }
}
